<?php


namespace app\admin\controller;

use app\admin\services\hwObs\Obs;
use think\admin\Controller;

/**
 * 门店设置
 * Class Distributor
 * @package app\admin\controller
 */
class Distributor extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'lgDistributor';

    private $type = [
        1 => '自营',
        2 => '联营',
        3 => '加盟',
    ];
    private $status = [
        1 => '正常',
        2 => '关闭'
    ];

    protected $rule = [
        'encrypt' => 'require',
        'name' => 'require',
        'province' => 'require',
        'city' => 'require',
        'district' => 'require',
        'address' => 'require',
        'tel' => 'require',
        'partner_phone' => 'mobile',
        'type' => 'require|in:1,2,3',
        'status' => 'require|in:1,2',
    ];

    protected $msg = [
        'encrypt.require' => '请输入门店编码',
        'name.require' => '请输入门店名称',
        'province.require' => '请选择省',
        'city.require' => '请选择市',
        'district.require' => '请选择省区',
        'address.require' => '请输入门店地址',
        'tel.require' => '请输入门店联系电话',
        'partner_phone.mobile' => '请输入正确格式的手机号',
        'type.require' => '请选择门店类别',
        'type.in' => '请选择正确的类别',
        'status.require' => '请选择门店状态',
        'status.in' => '请选择正确的状态',
    ];
    
    /**
     * 门店管理列表
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        //标题
        $this->title = '门店管理';
        //省查询
        $provinces = $this->cityArray(1);
        $this->assign('province', $provinces);
        //链接数据库
        $text = input('text');
        $where = [];
        if ($text) {
            $where[] = ['name|encrypt', 'like', '%' . $text . '%'];
        }
        $query = $this->_query($this->table)->dateBetween('create_at');
        $query->where($where)->equal('status')->equal('type');
        $query->equal('province')->equal('city')->equal('district');
        $query->order('id desc')->page();
    }

    protected function _page_filter(&$list)
    {
        foreach ($list as $key => $value) {
            $value['type'] = $this->type[$value['type']] ?? '-';
            $value['status'] = $this->status[$value['status']] ?? '-';
            $list[$key] = $value;
        }
    }

    /**
     * 添加门店
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->_applyFormToken();
        //城市查询
        $provinces = $this->cityArray(1);
        $this->assign('province', $provinces);

        //考勤
        $check_work_attendance = $this->app->db->table('lg_check_work_attendance')->order('id asc')->column('name', 'id');
        $this->assign('check_work_attendance', $check_work_attendance);

        //验证器
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $vailResult = $this->app->validate->message($this->msg)->check($data, $this->rule);
            if ($vailResult !== true) {
                $this->error($this->app->validate->getError());
            }
        }
        sysoplog('门店管理', '添加数据成功');
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑门店
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_applyFormToken();
        //城市查询
        $provinces = $this->cityArray(1);
        $this->assign('province', $provinces);
        //考勤
        $check_work_attendance = $this->app->db->table('lg_check_work_attendance')->order('id asc')->column('name', 'id');
        $this->assign('check_work_attendance', $check_work_attendance);

        //验证器
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $vailResult = $this->app->validate->message($this->msg)->check($data, $this->rule);
            if ($vailResult !== true) {
                $this->error($this->app->validate->getError());
            }
        }

        sysoplog('门店管理', '编辑数据成功');
        $this->_form($this->table, 'form');
    }

    protected function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            if (isset($params['id'])) {
                $data['update_time'] = time();
            } else {
                $data['create_time'] = time();
                $data['update_time'] = time();
            }
        }
    }


    /**
     * 获取省市区
     * @param int|null $type
     * @param int|null $city
     * @return array|\think\response\Json
     */
    public function cityArray(int $type = null, int $city = null)
    {
        $where = [];
        $pid = input('pid');
        if ($pid) {
            $where[] = ['parent_id', '=', $pid];
            $data = $this->app->db->table('lg_citys')->where($where)->order('id asc')->column('name', 'id');
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $data]);
        } else {
            if ($type) {
                $where[] = ['level_type', '=', $type];
            }
            if ($city) {
                $where[] = ['id', '=', $city];
            }
            return $this->app->db->table('lg_citys')->where($where)->order('id asc')->column('name', 'id');
        }
    }

    /**
     * 删除门店
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        sysoplog('门店管理', '删除数据成功');
        $this->_delete($this->table);
    }
}